import puppeteer, { LaunchOptions, Page } from 'puppeteer';
import * as path from 'path';

const lauchOptions: LaunchOptions = {
    channel: undefined,
    executablePath: undefined,
    ignoreDefaultArgs: false,
    enableExtensions: false,
    handleSIGINT: true,
    handleSIGTERM: true,
    handleSIGHUP: true,
    timeout: 0,
    dumpio: undefined,
    env: undefined,
    pipe: undefined,
    browser: undefined,
    extraPrefsFirefox: undefined,
    waitForInitialPage: undefined,
    headless: false,
    userDataDir: path.join(process.cwd(), 'browser-data'),
    devtools: false,
    debuggingPort: undefined,
    args: undefined,

    // ConnectOptions
    acceptInsecureCerts: undefined,
    networkEnabled: undefined,
    downloadBehavior: undefined,
    slowMo: undefined,
    targetFilter: undefined,
    protocol: undefined,
    protocolTimeout: undefined,
    browserWSEndpoint: undefined,
    browserURL: undefined,
    transport: undefined,
    headers: undefined,
    capabilities: undefined
};

(async () => {
    const browser = await puppeteer.launch(lauchOptions);

    const page = await browser.newPage();
    const uid = '253350665';
    const { thumbnail, nickname, description, fansAmount, videos } = await fetchPersonInfo(page, uid);
    console.log(uid, thumbnail, nickname, description, fansAmount, videos);
})();

async function fetchPersonInfo(page: Page, uid: string) {
    await page.goto(`https://space.bilibili.com/${uid}`);

    await page.waitForSelector('.b-avatar__layer__res');
    const thumbnail = await page.$eval('.b-avatar__layer__res', (el) => {
        return el.children[0].firstElementChild?.getAttribute('srcset');
    });
    const nickname = await page.$eval('.nickname', (el) => {
        return el.textContent;
    });
    const description = await page.$eval('.upinfo-detail__bottom .pure-text', (el) => {
        return el.textContent;
    });
    const fansAmount = await page.$$eval('.nav-statistics__item', (els) => {
        return els.map((el) => {
            const sencondDom = el.querySelector('.nav-statistics__item-num');
            const fans = sencondDom?.getAttribute('title')
            return fans;
        });
    });
    await page.waitForSelector('.video-section');
    const videoSection = await page.$('.video-section');
    const videos = await videoSection?.$$eval('.items .items__item', (els) => {
        console.log(els.length);
        return els.map((el) => {
            const thumbDom = el.querySelector('.bili-cover-card');
            const BVid = thumbDom?.getAttribute('href')?.split('/').find((item) => {
                return item.match(/^BV\w+$/);
            });
            const imageDom = thumbDom?.querySelector('img');
            const videoCover = imageDom?.getAttribute('src');
            const videoTitle = imageDom?.getAttribute('alt');
            return {
                BVid,
                videoCover,
                videoTitle,
            };
        });
    });
    return {
        uid,
        thumbnail,
        nickname,
        description,
        fansAmount,
        videos,
    };
}